package cn.jinjimi.app.main.widget.biz;

import android.content.Context;
import android.view.LayoutInflater;
import android.widget.HorizontalScrollView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cn.jinjimi.base.util.DateUtils;
import cn.jinjimi.app.R;
import cn.jinjimi.app.custom.entity.ClueStatistic;
import cn.jinjimi.app.main.widget.ChartView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Roy
 * Date: 17/1/3
 */

public class CustomClueHeadView extends RelativeLayout {
    private ChartView mChartView;
    private HorizontalScrollView mScrollView;

    private TextView tvTolNum;
    private TextView tvSaveNum;
    private TextView tvUnsaveNum;

    public CustomClueHeadView(Context context) {
        super(context);
        LayoutInflater.from(getContext()).inflate(R.layout.custom_clue_head_view, this);

        initView();
        initData();
    }

    private void initView() {
        tvTolNum = (TextView) findViewById(R.id.tv_total_custom_num);
        tvSaveNum = (TextView) findViewById(R.id.tv_save_num);
        tvUnsaveNum = (TextView) findViewById(R.id.tv_unsave_num);

        mChartView = (ChartView) findViewById(R.id.chart);
        mScrollView = (HorizontalScrollView) findViewById(R.id.hsv);
    }

    private void initData() {
        final List<ChartView.Data> mData = new ArrayList<>();
        for (int i = -12; i <= 0; i++) {
            ChartView.Data a = new ChartView.Data(DateUtils.getYearMonthBefore(i), 0);
            mData.add(a);
        }
        final ChartView.Data m = new ChartView.Data(DateUtils.getNextMonthStr(), 0);
        m.setUnReach(true);
        mData.add(m);
        ChartView.Adapter mAdpter = new ChartView.Adapter(mData);
        mChartView.setAdapter(mAdpter);

    }

    public void onDestroy() {
        if (mChartView != null)
            mChartView.onDestroy();
    }

    public void setData(ClueStatistic clueStatistic) {
        if (clueStatistic != null) {
            tvTolNum.setText(String.valueOf(clueStatistic.totalClueNum));
            tvSaveNum.setText(String.valueOf(clueStatistic.saveClueNum));
            tvUnsaveNum.setText(String.valueOf(clueStatistic.unsaveClueNum));

            List<ChartView.Data> mData = new ArrayList<>();
            String last = null;
            for (ClueStatistic.Monthly monthly : clueStatistic.monthlies) {
                ChartView.Data data = new ChartView.Data(monthly.month, monthly.clues);
                mData.add(data);
                last = monthly.month;
            }
            ChartView.Data m = new ChartView.Data(DateUtils.getYearMonthAfter(last), 0);
            m.setUnReach(true);
            mData.add(m);
            ChartView.Adapter mAdpter = new ChartView.Adapter(mData);
            mChartView.setAdapter(mAdpter);
        }
    }
}
